<!--用户的反馈添加与修改-->
<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? '新增反馈' : '编辑反馈'" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
             label-width="120px">
      <el-form-item prop="feedback" label="内容">
        <el-input v-model="dataForm.feedback" type="textarea" placeholder="请输入内容"></el-input>
      </el-form-item>
      <el-form-item prop="phone" label="电话号码">
        <el-input v-model="dataForm.phone" placeholder="请输入电话号码"></el-input>
      </el-form-item>
      <el-form-item prop="url1" label="图片">
        <div class="flex_center flex_wrap">
          <div class="img_con" v-if="dataForm.url1">
            <el-image class="deduct_img" :src="dataForm.url1" :preview-src-list="[dataForm.url1]"/>
            <i class="el-icon-error remove_img" @click="cancelImg(dataForm.url1,1)"/>
          </div>
          <com-upload v-show="!dataForm.url1" @get-url="getUrlOne"></com-upload>
        </div>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: '',
        feedback: '',	        //内容
        phone: '',            //电话号码
        url1: '',             //图片
      }
    }
  },
  computed: {
    dataRule() {
      return {
        feedback: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        phone: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/api/userFeedback/${this.dataForm.id}`).then(({data: res}) => {
        //console.log(res)
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/api/userFeedback', this.dataForm).then(({data: res}) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {
        })
      })
    }, 1000, {'leading': true, 'trailing': false}),
    // 添加单图添加
    getUrlOne(getUrl){
      this.dataForm.url1 = getUrl
    },
    // 删除服务器图片
    cancelImg(url,index) {
      this.$http({
        url: `/sys/oss/deleteByUrl`,
        method: 'delete',
        data: {url: url},
      }).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        index===1?this.dataForm.url1 = '':''
        // index===2?this.dataForm.video = '':''
        // index===3?this.dataForm.img = '':''
        // index===4?this.dataForm.logo = '':''
        if (this.dataForm.id){
          this.$http['put']('/api/userFeedback', this.dataForm).then(({data: res}) => {
          }).catch((err) => {
            this.$message.error(err.msg)
          })
        }
      }).catch(() => {})
    },
  }
}
</script>
<style>
.el-upload el-upload-picture-card {
  display: none;
}

.img_con {
  position: relative;
  display: flex;
}

.deduct_img {
  width: 140px;
  height: 140px;
  margin-right: 10px;
  margin-bottom: 10px;
}

.remove_img {
  position: absolute;
  right: 5px;
  top: -5px;
  color: red;
  cursor: pointer;
}
</style>
